var pageNum = 1;
$(function(){
    getCouponList();
});
//获取数据
function getCouponList () {
    $.ajax({
        type:"get",
        url: upUrl + "/coupon/front/couponList",
        dataType: "json",
        data: {
            pageNum: pageNum,
            pageSize: 10000,
        },
        success:function(data){
            var testList = data.result.records;
            total = data.result.total;
            var orderBody = '';
            $.each(testList, function (index, val) {
                if (val.cflag == '1') {
                    orderBody += '<div class="c_title">\
                    <div class="item_left">\
                        <div class="left_1">' + val.name + '</div>\
                         <div class="left_2">' + val.amount + ' 元</div>\
                    </div>\
                    <div class="item_center">\
                        ' + val.introduction + '\
                    </div>\
                    <div class="item_right">\
                        <span class="received">已领用</span>\
                    </div>\
                </div>'
                } else {
                    orderBody += '<div class="c_title">\
            <div class="item_left">\
                <div class="left_1">' + val.name + '</div>\
                <div class="left_2">' + val.amount + ' 元</div>\
            </div>\
            <div class="item_center">\
                ' + val.introduction + '\
            </div>\
            <div class="item_right">\
                <span onclick="addClick(' + val.id + ')" class="now">立即领用</span>\
            </div>\
        </div>'
                }

            })
            $('.c_box').html(orderBody);
        }
    })
}
//优惠券领用事件
function addClick(value){
    $.ajax({
        type:"post",
        url: upUrl + "/receiveCoupon/addReceiveCoupon",
        data: {
            cid: value,
        },
        success:function(data){
            if (data.code == 200) {
                toastr.success("领用成功!");
                getCouponList();
            } else {
                toastr.error(data.message);
            }
        }
    })
}